package org.com.money.controller;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridBagLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.Border;

import org.com.money.api.GetMoneyService;
import org.com.money.componet.Msg;
import org.com.money.core.GetMoneyServiceImpl;
import org.com.money.dto.GetMoneyDTO;
import org.com.money.service.MoneyException;

/***
 * 銀行控制器
 * 
 * @author tsai
 * 
 */
public class BankController extends Context implements ActionListener {

	private transient GetMoneyDTO dto = new GetMoneyDTO();

	// ----ui---//

	private transient GetMoneyService getMoneyService = new GetMoneyServiceImpl();

	private JButton getMoneyBtn = new JButton();
	private JButton loginBtn = new JButton();
	// private JTextField totalMoney = new JTextField("100");
	private JTextField getMoney = new JTextField("10");
	private JTextField passowrd = new JTextField(10);
	private JTextField userId = new JTextField(10);
	private JPanel loginPanel1 = new JPanel();
	private JPanel getPanel2 = new JPanel();
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private void intiPage() {

		final Dimension screenSize = Toolkit.getDefaultToolkit()
				.getScreenSize();
		final int screenWidth = screenSize.width;
		final int screenHeight = screenSize.height;
		final Dimension frameSize = this.getSize();
		int x = (screenWidth - frameSize.width) / 2;
		int y = (screenHeight - frameSize.height) / 2;
		this.setLocation(x, y);
		this.setSize(500, 200);

		//
		this.setLayout(new BorderLayout());
		this.setTitle("銀行登入系統");

		// this.totalMoney.setColumns(10);
		this.getMoney.setColumns(10);

		this.getMoneyBtn.setText("提款");
		this.loginBtn.setText("登入");
		// this.totalMoney.setText("");
		this.getMoney.setText("0");

		//
		// panel.add(this.totalMoney);
		getPanel2.add(this.getMoneyBtn);
		getPanel2.add(this.getMoney);

		loginPanel1.add(this.loginBtn);
		loginPanel1.add(this.passowrd);
		loginPanel1.add(this.userId);

		this.add(getPanel2, BorderLayout.CENTER);
		this.add(loginPanel1, BorderLayout.NORTH);
		// this.add(getPanel);

		getPanel2.setVisible(false);
		this.setVisible(true);

		loginBtn.addActionListener(this);
		getMoneyBtn.addActionListener(this);
		// this.setVisible(true);//

	}

	public BankController() {
		super();
		this.intiPage();

	}

	private void handMessage(Exception e) {
		if (e instanceof MoneyException) {
			MoneyException ex = (MoneyException) e;
			JOptionPane.showMessageDialog(null, ex.getExMsg());
		} else {
			JOptionPane.showMessageDialog(null, Msg.N001.getExMessage());
		}

	}

	@Override
	public void actionPerformed(ActionEvent e) {
		try {

			if (e.getSource() == loginBtn) {

				this.dto.setId(this.userId.getText());
				this.dto.setPassword(this.passowrd.getText());

				System.out.println(this.dto);

				this.getMoneyService.login(dto);
				JOptionPane.showMessageDialog(null, "登入成功！目前總金額:"
						+ dto.getUser().getTotalMoney());
				getPanel2.setVisible(true);

			} else {

				dto.setDrawWith(Integer.parseInt(this.getMoney.getText()));
				this.getMoneyService.getMoney(dto);
				JOptionPane.showMessageDialog(null, "剩下金額:"
						+ dto.getUser().getTotalMoney());
				// this.totalMoney.setText(dto.getUser().getTotalMoney() + "");

			}
		} catch (Exception ex) {
			this.handMessage(ex);
		}
		// TODO Auto-generated method stub

	}

}
